#include "ConfigApp.h"
#include "WirelessProtocols/Console.h"
#include "Transceivers/Transceivers.h"
#include "WirelessProtocols/SymbolTime.h"
#include "Transceivers/Security.h"
#include "WirelessProtocols/MCHP_API.h"

#include "TratamentoAlicate.h"
#include <i2c.h>
#include "HardwareProfile.h"

BOOL gravarAlicates(void) {
    int vetorHorario[7];
    int pagina, posPagina;

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xD0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xD1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    vetorHorario[0] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[1] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[2] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[3] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[4] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[5] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[6] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);
    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xA0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xA1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    pagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    posPagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0) | ((pagina << 1)));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(posPagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0xFF);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    int countTemp = 0;
    for (countTemp; countTemp < 7; countTemp++) {
        MasterWriteI2C1(vetorHorario[countTemp]);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
    }
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    if ((posPagina + 8) > 255) {
        pagina++;
        if (pagina > 7) {
            pagina = 0;
        }
    }
    posPagina = posPagina + 8;

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xA0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(pagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(posPagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xCC);
    while (I2C1STATbits.TBF);
    MasterWriteI2C1(0xAA);
    while (I2C1STATbits.TBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);
    while (RX == 1);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xA0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xA1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    pagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    posPagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0) | ((pagina << 1)));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(posPagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0xFF);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    if ((posPagina + 1) > 255) {
        pagina++;
        if (pagina > 7) {
            pagina = 0;
        }
    }
    posPagina = posPagina + 1;

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xA0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(pagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(posPagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    return TRUE;
}

BOOL gravarAlicates2(void) {
    int vetorHorario[7];
    int pagina, posPagina;

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xD0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xD1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    vetorHorario[0] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[1] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[2] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[3] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[4] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[5] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    vetorHorario[6] = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x02);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    int countTemp = 0;
    for (countTemp; countTemp < 7; countTemp++) {
        MasterWriteI2C1(vetorHorario[countTemp]);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
    }
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x09);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xCC);
    while (I2C1STATbits.TBF);
    MasterWriteI2C1(0xAA);
    while (I2C1STATbits.TBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);
    while (RX == 1);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1(0xA0);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    RestartI2C1();
    while (I2C1CONbits.RSEN);
    MasterWriteI2C1(0xA1);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    pagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    AckI2C1();
    while (I2C1CONbits.ACKEN);
    posPagina = MasterReadI2C1();
    while (I2C1STATbits.RBF);
    NotAckI2C1();
    while (I2C1CONbits.ACKEN);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0)); // | ((pagina << 1)));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(posPagina);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0xFF);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    IdleI2C1();
    StartI2C1();
    while (I2C1CONbits.SEN);
    MasterWriteI2C1((0xA0));
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x00);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    MasterWriteI2C1(0x02);
    while (I2C1STATbits.TBF);
    while (I2C1STATbits.ACKSTAT);
    IdleI2C1();
    StopI2C1();
    while (I2C1CONbits.PEN);

    return TRUE;

}
